package com.cskaoyan.service;


import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.category.CategoryVo;
import com.cskaoyan.bean.domain.Category;
import com.cskaoyan.bean.domain.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 类目表 服务实现类
 * </p>
 *
 * @author looper
 * @since 2021-11-29
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    CategoryMapper mapper;

    @Override
    public BaseData getCategory(Integer i) {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andLevelEqualTo("L" + i);
        //逻辑删除单独处理
        criteria.andDeletedEqualTo(false);
        //查询pid为0的所有目录
        List<Category> categories = mapper.selectByExample(example);
        ArrayList<CategoryVo> categoryVos = new ArrayList<>();
        CategoryVo categoryVo;
        //遍历查询,并存放到vo中
        for (int j = 0; j < categories.size(); j++) {
            categoryVo = new CategoryVo();
            categoryVo.setValue(categories.get(j).getId());
            categoryVo.setLabel(categories.get(j).getName());
            categoryVos.add(categoryVo);
        }

        return BaseData.getBack(categories.size(), i, i, categories.size(), categoryVos);
    }

    @Override
    public BaseData getList() {
        //套娃,先查pid为0的所有,再查所有pid不为0的,然后循环添加一次
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<Category> fatherList = mapper.selectByExample(example);
        //pid不为0的二级目录
        CategoryExample sonExample = new CategoryExample();
        CategoryExample sonExample1 = sonExample;
        CategoryExample.Criteria criteria1 = sonExample1.createCriteria();

        criteria1.andPidNotEqualTo(0);
        criteria1.andDeletedEqualTo(false);
        List<Category> sonList = mapper.selectByExample(sonExample1);
        List<Category> listCategory = getListCategory(fatherList, sonList);

        return BaseData.getBack(listCategory.size(), 1, 1, listCategory.size(), listCategory);


    }

    @Override
    public void getUpate(Category category) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdEqualTo(category.getId());
        mapper.updateByExampleSelective(category,categoryExample);
    }

    @Override
    public Category getInsert(Category category) {
        category.setAddTime(new Date());
        //插入一个并且返回
        mapper.insertSelective(category);
        //根据返回的id查询
        Category byPrimaryKey = mapper.selectByPrimaryKey(category.getId());

        return  byPrimaryKey;

    }

    @Override
    public Integer getDeleteById(Integer id) {

        //更新uptime
        Date date = new Date();
        mapper.updatetimeById(id,date);

        //逻辑删除 -->改为真实删除
//        CategoryExample categoryExample = new CategoryExample();
//        categoryExample.createCriteria().andIdEqualTo(id);
//        mapper.deleteByExample(categoryExample);
        mapper.deleteByUpdate(id);

        return 200;
    }

    private List<Category> getListCategory(List<Category> father, List<Category> son) {
        for (Category fatherobj : father) {
        ArrayList<Category> list = new ArrayList();
            for (Category sonobj : son) {
                if ((fatherobj).getId().equals((sonobj).getPid())) {
                    list.add(((Category) sonobj));
                }
            }
                fatherobj.setChildren(list);
        }
        return father;
    }

}
